<template>
  <div>
    <el-table :data="tableDataList" style="width: 100%;margin-top: 15px;" height="500" :header-cell-style="headerCellStyle" :cell-style="cellStyle">
      <template slot="empty">
        <p>暂无商品</p>
      </template>
      <el-table-column prop="sort" align="center" label="序号" min-width="10%">
      </el-table-column>
      <el-table-column prop="image" :show-overflow-tooltip="true" align="center" label="商品图片" min-width="10%">
        <template slot-scope="scope">
          <img :src="scope.row.productPhoto" width="64" height="64">
        </template>
      </el-table-column>
      <el-table-column prop="productName" :show-overflow-tooltip="true" align="center" label="商品名称" min-width="25%">
      </el-table-column>
      <el-table-column prop="price" :show-overflow-tooltip="true" align="center" label="商品价格" min-width="10%">
      </el-table-column>
      <el-table-column prop="clickNum" :show-overflow-tooltip="true" align="center" label="点击次数" min-width="10%">
      </el-table-column>
      <el-table-column prop="pushNum" :show-overflow-tooltip="true" align="center" label="推送次数" min-width="10%">
      </el-table-column>
      <el-table-column align="center" label="操作" min-width="25%">
        <template slot-scope="scope">
          <!--                <el-button type="text" size="mini">上移</el-button>-->
          <!--                <el-button type="text" size="mini">下移</el-button>-->
          <el-button type="text" v-if="scope.row.isTop == 0" size="mini" @click="pushScreen(scope.row)">推屏</el-button>
          <el-button type="text" disabled v-if="scope.row.isTop == 1" size="mini">{{scope.row.countdown>0?scope.row.countdown+'s':'推送中'}}</el-button>
          <el-button type="text" size="mini" v-if="scope.row.isTop == 0" @click="del(scope.row)">删除</el-button>
          <!--                <el-button type="text" v-if="scope.row.isTop == 1" size="mini" @click="cancelPush(scope.row)">取消</el-button>-->
        </template>
      </el-table-column>
    </el-table>
    <div style="padding: 15px;text-align: right;">
      <el-pagination
        layout="total, prev, pager, next, jumper,sizes"
        @current-change="refreshPageRequest"
        :current-page="pageRequest.pageNum"
        :page-size="pageRequest.pageSize"
        :total="totalSize"
        :page-sizes="[10, 20, 30, 40]"
        @size-change="handleSizeChange"
      ></el-pagination>
    </div>
    <el-dialog
      title="商品添加"
      :visible.sync="dialogVisible"
      width="70%"
      @opened="getShopAddTableData"
      append-to-body>
      <el-form label-width="100px" size="mini" :model="dataForm">
        <el-row v-if="isLyfen">
          <el-col :span="8">
            <el-form-item label="商品名称：">
              <el-input v-model="dataForm.skuName"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="商品编号：">
              <el-input v-model="dataForm.skuCode"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="渠道编码：">
              <cy-select filterable url="/api/v1/broadcast/getShopSelectChannelList" v-model="dataForm.channelCode"></cy-select>
            </el-form-item>
          </el-col>
          <el-col :offset="16" :span="8">
            <el-form-item>
              <el-button type="primary" @click="getAddTableData">查询</el-button>
              <el-button @click="reset">重置</el-button>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <el-table :data="addTableData" v-loading="loading" @selection-change="confirm" ref="addTableData" style="width: 100%;" height="400" :header-cell-style="headerCellStyle" :cell-style="cellStyle">
        <template slot="empty">
          <p>暂无商品</p>
        </template>
        <el-table-column type="selection" width="55">
        </el-table-column>
        <el-table-column prop="skuCode" :show-overflow-tooltip="true" align="center" label="商品编号" min-width="10%">
        </el-table-column>
        <el-table-column prop="productPic" :show-overflow-tooltip="true" align="center" label="商品图片" min-width="20%">
          <template slot-scope="scope">
            <img :src="scope.row.productPic" width="64" height="64">
          </template>
        </el-table-column>
        <el-table-column prop="channelSkuName" :show-overflow-tooltip="true" align="center" label="商品名称" min-width="20%">
        </el-table-column>
        <el-table-column prop="price" :show-overflow-tooltip="true" align="center" label="商品价格" min-width="10%">
        </el-table-column>
        <el-table-column prop="categoryValue" :show-overflow-tooltip="true" align="center" label="类目" min-width="20%">
        </el-table-column>
      </el-table>
      <div style="padding: 15px;text-align: right;">
        <el-pagination
          layout="total, prev, pager, next, jumper,sizes"
          @current-change="refreshPageRequest2"
          :current-page="pageRequest2.pageNum"
          :page-size="pageRequest2.pageSize"
          :total="totalSize2"
          :page-sizes="[10, 20, 30, 40]"
          @size-change="handleSizeChange2"
          :disabled="loading"
        ></el-pagination>
      </div>
      <!--<div style="text-align: center;">-->
      <!--<el-button size="mini" @click="cancel">取消</el-button>-->
      <!--<el-button size="mini" type="primary" @click="cancel">确定</el-button>-->
      <!--</div>-->
    </el-dialog>
  </div>
</template>
<script>
  import CySelect from "../../component/CySelect";
  export default {
    name: "liveControllerShopManage",
    components: {
      CySelect,
    },
    props:{
      data:{
        type: Object
      }
    },
    data() {
      return {
        isLyfen: false,
        dialogVisible: false,
        headerCellStyle:{
          'background-color': '#f1f1f1',
          'font-size':'12px',
          'color':'#000000'
        },
        cellStyle:{
          'font-size':'12px'
        },
        dataForm:{},
        tableDataList:[],
        addTableData:[],
        pageRequest:{
          pageNum:1,
          pageSize:10
        },
        totalSize:0,
        pageRequest2:{
          pageNum:1,
          pageSize:10
        },
        totalSize2:0,
        parentData:null,
        loading: false,
        timer: null
      }
    },
    watch:{
      dialogVisible:function (newVal,oldVal) {
        if(!newVal){
          this.resetAddData();
        }
      }
    },
    mounted: function () {
      this.parentData = this.data
      if(this.parentData){
        this.getTableData()
        this.startTimer()
      }
      this.getIsLyFen();
    },
    destroyed(){ // 页面销毁
      this.clearTimer()
    },
    methods: {
      init(data){ // 初始化
        this.parentData = data
        this.getTableData()
        this.startTimer()
      },
      startTimer(){ // 定时器
        let i=0;
        this.timer = setInterval(() => {
          this.tableDataList.forEach((item,idx) => {
            if(item.isTop==1 && item.countdown>0){
              item.countdown = item.countdown - 1 <= 0 ? 0 : item.countdown - 1;
            } else if(item.isTop==1 && item.countdown == 0){
              this.getTableData()
            }
          })
          if(i==5){
            this.getTableData();
            i=0;
          }
          i++;
        },1000)
      },
      clearTimer(){ // 清除定时器
        clearInterval(this.timer)
      },
      resetAddData(){ // 重置添加商品数据
        this.dataForm = {}
        this.addTableData = []
        this.pageRequest2 = {
          pageNum:1,
          pageSize:10
        }
      },
      getTableData(){ // 获取表格信息
        let params = {
          pageNum: this.pageRequest.pageNum,
          pageSize: this.pageRequest.pageSize,
          liveId: this.parentData.id,
          number:this.parentData.number
        }
        this.utils.request.liveRequest({
          url: '/api/v1/liveProductInfo/list',
          method: 'get',
          params: params
        }).then(result => {
          let data = result.data;
          if (data && data.code === '0') {
            this.tableDataList = data.data.rows;
            this.totalSize = data.data.total;
          }
        })
      },
      getIsLyFen(){ // 获取表格信息
        this.utils.request.liveRequest({
          url: '/api/v1/liveProductInfo/getIsLyfen',
          method: 'get',
        }).then(result => {
          let data = result.data;
          if (data && data.code === '0') {
            this.isLyfen = data.data;
          }
        })
      },
      getAddTableData(){ // 获取添加商品表格数据
        let params = {
          pageNum: this.pageRequest2.pageNum,
          pageSize: this.pageRequest2.pageSize,
          skuCode:this.dataForm.skuCode,
          skuName:this.dataForm.skuName
        }
        this.loading = true
        this.utils.request.liveRequest({
          url: '/api/v1/liveProductInfo/list/'+this.dataForm.channelCode,
          method: 'post',
          params: params
        }).then(result => {
          this.loading = false
          let data = result.data;
          if (data && data.code === '0') {
            this.addTableData = data.data.rows;
            this.totalSize2 = data.data.total;
          }
        })
          .catch(() => {
            this.loading = false
          });
      },
      getShopAddTableData(){ // 获取添加商品表格数据
        let params = {
          pageNum: this.pageRequest2.pageNum,
          pageSize: this.pageRequest2.pageSize,
          skuCode:this.dataForm.skuCode,
          skuName:this.dataForm.skuName
        }
        if(this.isLyfen){
          return;
        }
        this.loading = true
        this.utils.request.liveRequest({
          url: '/api/v1/liveProductInfo/shopChannelList',
          method: 'post',
          params: params
        }).then(result => {
          this.loading = false
          let data = result.data;
          if (data && data.code === '0') {
            this.addTableData = data.data.rows;
            this.totalSize2 = data.data.total;
          }
        })
          .catch(() => {
            this.loading = false
          });
      },
      reset(){ // 重置
        this.dataForm = {}
      },
      handleSizeChange: function (pageSize) {
        this.pageRequest.pageSize = pageSize;
        this.pageRequest.pageNum = 1;
        this.getTableData()
      },
      refreshPageRequest: function (pageNum) {
        this.pageRequest.pageNum = pageNum;
        this.getTableData()
      },
      handleSizeChange2: function (pageSize) {
        this.pageRequest2.pageSize = pageSize;
        this.pageRequest2.pageNum = 1;
        if (this.isLyfen) {
          this.getAddTableData();
        }else{
          this.getShopAddTableData();
        }
      },
      refreshPageRequest2: function (pageNum) {
        this.pageRequest2.pageNum = pageNum;
        if (this.isLyfen) {
          this.getAddTableData();
        }else{
          this.getShopAddTableData();
        }

      },
      cancel(){
        this.dialogVisible = false
      },
      confirm(){ // 确定
        let selection = this.$refs.addTableData.selection;
        if(selection.length>0){
          let liveId = this.parentData.id;
          this.utils.request.liveRequest({
            url: '/api/v1/liveProductInfo/batchSave/'+liveId,
            method: 'post',
            params: selection
          }).then(result => {
            let data = result.data;
            if (data && data.code === '0') {
              this.$message.success("添加成功!");
              this.getTableData()
            } else {
              this.$message.error("添加失败!");
            }
          })
        }
      },
      handleRowClick(row){
        this.$refs.addTableData.toggleRowSelection(row);
      },
      pushScreen(row){ // 推屏
        let params = {
          id: row.id,
          liveId: this.parentData.id
        }
        this.utils.request.liveRequest({
          url: '/api/v1/liveProductInfo/push',
          method: 'post',
          params: params
        }).then(result => {
          let data = result.data;
          if (data && data.code === '0') {
            this.$emit("pushScreen",row)
            this.getTableData()
          }
        })
      },
      cancelPush(row){
        let params = {
          id: row.id,
          liveId: this.parentData.id
        }
        this.utils.request.liveRequest({
          url: '/api/v1/liveProductInfo/cancelPush',
          method: 'post',
          params: params
        }).then(result => {
          let data = result.data;
          if (data && data.code === '0') {
            this.$emit("pushScreen",row)
            this.getTableData()
          }
        })
      },
      del(row){
        this.$confirm('你确定要删除此项吗', '提示', {
          type: 'warning'
        }).then(() => {
          let params = {
            id: row.id,
            liveId: this.parentData.id
          }
          this.utils.request.liveRequest({
            url: '/api/v1/liveProductInfo/delete',
            method: 'get',
            params: params
          }).then(result => {
            let data = result.data;
            if (data && data.code === '0') {
              this.$message.success("删除成功!");
              this.getTableData()
            } else {
              this.$message.error("删除失败!");
            }
          })

        }).catch(() => {
        });
      }
    }
  }
</script>
<style scoped lang="scss">

</style>
